package com.zsl.picture.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsl.picture.mapper.UserMapper;
import com.zsl.picture.model.User;
import com.zsl.picture.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<User> info() {

        return userMapper.selectList(new QueryWrapper<User>());
    }

    @Cacheable(cacheNames = "user",key = "#id",unless = "#result ==null")
    @Override
    public User getOne(Long id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public int save(User user) {
        return userMapper.insert(user);
    }

    @Override
    public User valid(User user) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",user.getName());
        queryWrapper.eq("pass_word",user.getPassWord());
        List<User> users =  userMapper.selectList(queryWrapper);
        if(users.size()>0){
            return users.get(0);
        }
        return null;
    }
}
